package main

import (
	"bufio"
	"fmt"
	"io"
	"strings"
)

type iAdder func(int) (int, iAdder)

func adder(base int) iAdder {
	return func(v int) (int, iAdder) {
		return base + v, adder(base + v)
	}
}

/*
	测试一：斐波那契数列
*/
func fibonacci() intGen {
	a, b := 0, 1
	return func() int {
		a, b = b, a+b
		return a
	}
}

/*
	测试二：为函数实现接口
*/
type intGen func() int

func (g intGen) Read(p []byte) (n int, err error) {
	next := g()
	if next > 10000 {
		return 0, io.EOF
	}
	s := fmt.Sprintf("%d\n", next)
	return strings.NewReader(s).Read(p)
}

func printFile(reader io.Reader) {
	scanner := bufio.NewScanner(reader)
	for scanner.Scan() {
		fmt.Println(scanner.Text())
	}
}
func main() {
	a := adder(0)
	for i := 0; i < 10; i++ {
		var s int
		s, a = a(i)
		fmt.Printf("0+...+%d=%d\n", i, s)
	}
	fmt.Println("-----fibonacci-----")
	f := fibonacci()
	for i := 0; i < 10; i++ {
		fmt.Println(f())
	}
	fmt.Println("-----printFile-----")
	printFile(f)
}
